package com.ctgu.AutoIdentify.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ctgu.AutoIdentify.DTO.UserDTO;
import com.ctgu.AutoIdentify.common.Constants;
import com.ctgu.AutoIdentify.entity.User;
import com.ctgu.AutoIdentify.exception.ServiceException;
import com.ctgu.AutoIdentify.mapper.UserMapper;
import com.ctgu.AutoIdentify.service.IUserService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    private static final Log LOG = Log.get();

    @Resource
    private UserMapper userMapper;


    @Override
    public UserDTO login(UserDTO userDTO) {
        User one = getUserInfoLogin(userDTO);//从数据库中找用户信息
        if (one != null) {//如果找到了，把用户信息赋给DTO
            BeanUtil.copyProperties(one, userDTO, true);//把前一个对象相同属性赋给后一个对象
            userDTO.setRole(one.getRole());//copy失效的情况下用set、get赋值
            userDTO.setGrantstatus(one.getGrantstatus());
            System.out.println(userDTO.getRole());//防止copy失败，把赋值之后值打出来看看
        }
            else{
                throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
            }
            return  userDTO;
        }


    @Override
    public User register(UserDTO userDTO) {
        User user = new User();
        User one = getUserInfoRegister(userDTO);
        if (one == null) {    //不能直接复制one，必须新建一个user复制
//            BeanUtil.copyProperties(userDTO, user, true);我在登录中用该方法把User实体类中的同名属性赋值给DTO是可以的，但是这里反过来就不行
           user.setUsername(userDTO.getUsername());
           user.setPassword(userDTO.getPassword());
                user.setNickname(user.getUsername());//默认情况下昵称等于用户名

            save(user);    //  把copy完之后的用户对象存储到数据库
        } else {
            throw new ServiceException(Constants.CODE_600, "账号已存在");
        }
        return user;
    }

    @Override
    public void updatePassword(User user) {
        int update = userMapper.updatePassword(user);
        if (update < 1) {
            throw new ServiceException(Constants.CODE_600, "密码错误");
        }
    }

    //  登录查询
    private User getUserInfoLogin(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDTO.getUsername());
        queryWrapper.eq("password", userDTO.getPassword());
        User one;
        try {
            one = getOne(queryWrapper); // 从数据库查询用户信息
        } catch (Exception e) {
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }

    //  注册查询
    private User getUserInfoRegister(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDTO.getUsername());
        User one;
        try {
            one = getOne(queryWrapper); // 从数据库查询用户信息
        } catch (Exception e) {
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }




}

